package com.hhhn.wk.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hhhn.wk.R;
import com.hhhn.wk.utils.Constants;

import java.text.DecimalFormat;

public class PayDialog {
	static DecimalFormat df   = new DecimalFormat("######0.00");
	static int number=0;//计数数量
	static double zongji=0;//商品总计
	static double zongjiPrice=0;//最终价格

	static double ePriceOne=0;//最低价格
	static double ePriceTwo=0;
	static double ePriceThree=0;
	static int eNumberOne=0;//最低数量
	static int eNumberTwo=0;
	static int eNumberThree=0;
	static TextView tv_priceNew;
	static TextView tv_amount;
	static TextView tv_commodity_show_num;
	public interface OnActionSheetSelected {
		void onClick(int whichButton,int number,double zongjiPrice,double zongji);
	}

	private PayDialog() {
	}
	private static void  amount(){
		tv_commodity_show_num.setText(""+number);
		if (number>=eNumberOne &&number<eNumberTwo){
			tv_priceNew.setText("￥"+ePriceOne);
			zongjiPrice=ePriceOne;
			 zongji = ePriceOne * number;
			Log.d("RR", "amount: "+zongji);
			tv_amount.setText("总计："+df.format(zongji)+"元");
		}else if (number>=eNumberTwo &&number<eNumberThree){
			tv_priceNew.setText("￥"+ePriceTwo);
			 zongji = ePriceTwo * number;
			zongjiPrice=ePriceTwo;
			tv_amount.setText("总计："+df.format(zongji)+"元");
		}else if (number>=eNumberThree){
			tv_priceNew.setText("￥"+ePriceThree);
			 zongji = ePriceThree * number;
			zongjiPrice=ePriceThree;
			tv_amount.setText("总计："+df.format(zongji)+"元");
		}

	}
	public static Dialog showSheet(final Context context,
								   String commodityImg,
								   String commodityName,
								   String commodityprice1,
								   String commodityprice2,
								   String commodityprice3,
								   String commoditynum1,
								   String commoditynum2,
								   String commoditynum3,
								   final OnActionSheetSelected actionSheetSelected,
								   OnCancelListener cancelListener) {
		final Dialog dlg = new Dialog(context, R.style.ActionSheet);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.paydialog, null);
		final int cFullFillWidth = 10000;
		layout.setMinimumWidth(cFullFillWidth);
		ImageView iv_CommodityImg = (ImageView) layout.findViewById(R.id.iv_CommodityImg);
		TextView iv_CommodityName = (TextView) layout.findViewById(R.id.iv_CommodityName);
		tv_priceNew = (TextView) layout.findViewById(R.id.tv_priceNew);
		tv_amount = (TextView) layout.findViewById(R.id.tv_amount);
		tv_commodity_show_num = (TextView) layout.findViewById(R.id.tv_commodity_show_num);
		if (commoditynum1!=null||!commoditynum1.equals("")){
			number=(int)Double.parseDouble(commoditynum1);

			eNumberOne=(int)Double.parseDouble(commoditynum1);
			eNumberTwo=(int)Double.parseDouble(commoditynum2);
			eNumberThree=(int)Double.parseDouble(commoditynum3);
		}

		ePriceOne=Double.parseDouble(commodityprice1);
		ePriceTwo=Double.parseDouble(commodityprice2);
		ePriceThree=Double.parseDouble(commodityprice3);


		TextView tv_pay = (TextView) layout.findViewById(R.id.tv_pay);
		TextView iv_sub = (TextView) layout.findViewById(R.id.iv_sub);
		TextView iv_add = (TextView) layout.findViewById(R.id.iv_add);
		Glide.with(context) .load(Constants.HTTPIMG+commodityImg).into(iv_CommodityImg);
		tv_commodity_show_num.setText(""+number);
		iv_CommodityName.setText(""+commodityName);
		amount();
		//购买
		tv_pay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				actionSheetSelected.onClick(0,number,zongjiPrice,zongji);
				dlg.dismiss();
			}
		});
		//减
		iv_sub.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (number<=eNumberOne){
					//不可减
				}else {
					number--;

					amount();
					actionSheetSelected.onClick(1,number,zongjiPrice,zongji);
				}
			}
		});
		//+
		iv_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				number++;

				amount();
				actionSheetSelected.onClick(2,number,zongjiPrice,zongji);
			}
		});
		Window w = dlg.getWindow();
		WindowManager.LayoutParams lp = w.getAttributes();
		lp.x = 0;
		final int cMakeBottom = -1000;
		lp.y = cMakeBottom;
		lp.gravity = Gravity.BOTTOM;
		dlg.onWindowAttributesChanged(lp);
		dlg.setCanceledOnTouchOutside(false);
		if (cancelListener != null)
			dlg.setOnCancelListener(cancelListener);
		dlg.setContentView(layout);
		dlg.show();
		return dlg;
	}
}
